<?php
/**
*	上传文件接口
*/ 
class uploadClassAction extends apiAction
{

	/**
	*	上传文件
	*/
	public function upfileAction()
	{
		if(!$_FILES)exit('sorry!');
		$upimg		= c('upfile');
		$maxsize	= (int)$this->get('maxsize', $upimg->getmaxzhao());//上传最大M
		$uptypes	= getconfig('uptypes','image');
		$uptypes	= arrvalue($this->agentrs,'uptype');
		if(isempt($uptypes))$uptypes = '*';
		$uptypes	= str_replace(',','|', $uptypes);
		$updir		= $this->get('updir');
		if(isempt($updir)){
			$updir=date('Y-m');
		}else{
			$updir=str_replace(array(' ','.'),'', trim($updir));
			$updir=str_replace('{month}',date('Y-m'), $updir);
			$updir=str_replace('{Year}',date('Y'), $updir);
			$updir=str_replace(array('{','}'),'', $updir);
			$updir=str_replace(',','|', $updir);
		}
		$upimg->initupfile($uptypes, ''.UPDIR.'|'.$updir.'', $maxsize);
		$upses	= $upimg->up('file');
		if(!is_array($upses))exit($upses);
		$arr 	= c('down')->uploadback($upses,'', true,array(
			'agentid' => $this->agentid
		));
		return $arr;
	}
	
	
	
	/**
	*	上传时初始化看是不是存在文件
	*/
	public function initfileAction()
	{
		$filesize	= c('check')->onlynumber($this->post('filesize'));
		$fileext	= $this->post('fileext');
		$filename	= $this->getvals('filename');
		$filelastna	= $this->getvals('filelastna');
		$where 		= "`fileext`='$fileext' and `filesize`='$filesize' and `agentid`='$this->agentid'";
		if(!isempt($filename))$where.=" and `filename`='$filename'";
		$frs 		= m('file')->getone($where,'*','`id` desc');
		$bo 		= false;
		if($frs){
			$filepath 	 = $frs['filepath'];
			$filepathout = $frs['filepathout'];
			if(!isempt($filepath) && file_exists($filepath))$bo=true;
			if(!isempt($filepathout)){
				$bo=true; //存到外部如腾讯云
			}
		}
		if($bo){
			m('file')->update("`filelastna`='$filelastna'", $frs['id']);
			return returnsuccess($frs);
		}else{
			return returnerror('not found', 201);
		}
	}
	
	/**
	*	读取文件信息
	*/
	public function wjinfoAction()
	{
		$id = (int)$this->get('id','0');
		$frs = m('file')->getone($id);
		if($frs){
			return returnsuccess($frs);
		}else{
			return returnerror('not found', 201);
		}
	}

	
	/**
	*	上传base64的图片截图
	*/
	public function upcontAction()
	{
		$cont = $this->post('content');
		if(isempt($cont))exit('empty');
		$cont 	= str_replace(' ','', $cont);
		$cont	= $this->rock->jm->base64decode($cont);
		$name 	= $this->post('name');
		if(!$name)$name = lang('jietu');
		$arr 	= c('down')->createimage($cont,'png',$name,'',array(
			'agentid' => $this->agentid
		)); 
		return $arr;
	}
	
	
	/**
	*	获取文件信息
	*/
	public function fileinfoAction()
	{
		$lx 	= (int)$this->get('lx','0');
		$fileid = (int)$this->get('fileid','0');
		$fnum 	= $this->get('fnum');
		$where  = '`id`='.$fileid.'';
		if(!isempt($fnum))$where  = "`filenum`='$fnum'";
		
		$frs 	= m('file')->getone($where);
		if(!$frs)return returnerror(lang('notfound'),404);
		$filepathout = $frs['filepathout'];
		if(isempt($filepathout) && !file_exists($frs['filepath']))return returnerror(lang('notfile'),404); //文件不存在
		if($lx==3)return returnsuccess($frs);
		
		$fileid  = $frs['id'];
		$agentid = $this->agentid;
		$url	= '';
		$key1 	= rand(100000,999999).$this->jm->getRandkey();
		$key	= md5('abc'.$key1.'abce');
		$data['fileid']  = $fileid;
		$data['websign'] = $this->get('websign');
		$data['agentkey']= $this->agentkey;
		
		$sysname 		 = $this->jm->base64decode($this->get('sysname'));
		$sysface 		 = $this->jm->base64decode($this->get('sysface'));
		if(isempt($sysname))$sysname = $this->adminname;
		if(isempt($sysface))$sysface = $this->adminface;
		$sysuid 		 = $this->get('sysuid', $this->adminid);
		
		$data['userinfo']= array(
			'id' 	=> $sysuid,
			'name' 	=> $sysname,
			'face' 	=> $sysface,
		);
		c('cache')->set($key, $data,5*60);
		
		$whees 	= '`agentid`='.$agentid.' and `uid`='.$sysuid.'';
		$usar['agentid'] = $agentid;
		$usar['uid']  	 = $sysuid;
		$usar['name']    = $sysname;
		$usar['face']    = $sysface;
		$usar['optdt']   = $this->rock->now;
		$dbs 	= m('optus');
		if($dbs->rows($whees)==0)$whees = '';
		$dbs->record($usar, $whees);

		$fileext= $frs['fileext'];
		$yufile = ',gif,png,jpeg,jpg,docx,doc,xls,xlsx,ppt,pptx,pdf,txt,log,html,htm,js,php,php3,mp4,md,cs,sql,java,json,css,asp,aspx,shtml,cpp,c,vbs,jsp,xml,bat,ini,conf,sh,';
		if($lx==0 && !contain($yufile,','.$fileext.','))return returnerror(sprintf(lang('buzhic'),$fileext));
		
		if($lx==0){
			$url = ''.URL.'index.php?m=file&key='.$key.'';
		}
		if($lx==1){
			$url = ''.URL.'index.php?m=file&a=down&key='.$key.'';
		}
		if($lx==2){
			$url = ''.URL.'index.php?m=file&a=edit&key='.$key.'';
		}
		$isimg 	 = contain(',gif,png,jpeg,jpg,bmp,', ','.$fileext.',');
		if($isimg){
			$barr['thumbpath'] 	 = getface($frs['thumbpath']);
			$imgurl 	 		 = getface($frs['filepath']);
			if(!isempt($frs['thumbpathout'])){
				$barr['thumbpath'] = $frs['thumbpathout'];
				$barr['thumbpathout'] = $frs['thumbpathout'];
			}
			if(!isempt($frs['filepathout'])){
				$imgurl = $frs['filepathout'];
			}
			$barr['imgurl'] = $imgurl;
		}
		
		$barr['lx']  	 = $lx;
		$barr['type']  	 = $lx;
		$barr['url'] 	 = $url;
		$barr['fileext'] = $fileext;
		$barr['isimg'] 	 = $isimg;
		return returnsuccess($barr);
	}
	
	/**
	*	返回信息
	*/
	public function filesaveinfoAction()
	{
		$nums = $this->get('nums');
		$ids  = $this->get('ids');
		if(isempt($nums) && isempt($ids))return returnerror('无效编号');
		$where= '';
		if(!isempt($nums)){
			$nums = "'".str_replace(',',"','",$nums)."'";
			$where= "`filenum` in($nums)";
		}
		if(!isempt($ids)){
			$where= "`id` in($ids)";
		}
		$fobj = m('file');
		$rows = $fobj->getall($where);
		$arrs = array();
		foreach($rows as $k=>$rs){
			$arr  = array();
			$arr['filenum']   	= $rs['filenum'];
			$arr['filename']  	= $rs['filename'];
			$arr['fileext']   	= $rs['fileext'];
			$arr['filesize']  	= $rs['filesize'];
			$arr['filesizecn'] 	= $rs['filesizecn'];
			$arr['optname'] 	= $rs['optname'];
			$arr['adddt'] 		= $rs['adddt'];
			$arr['isimg'] 		= $fobj->isimg($rs['fileext']);
			if($arr['isimg']){
				$arr['thumbpath']  = $rs['thumbpath'];
				if(substr($arr['thumbpath'],0,4)!='http')$arr['thumbpath'] = '{FILEURL}'.$arr['thumbpath'].'';
				$arr['thumbpatho'] = $arr['thumbpath'];
				if(!isempt($rs['thumbpathout']))$arr['thumbpath'] = $rs['thumbpathout'];
			}
			if(!isempt($rs['filelastna']))$arr['filename']  = $rs['filelastna'];
			$arrs[] = $arr;
		}
		return returnsuccess($arrs);
	}
	
	/**
	*	删除文件
	*/
	public function filedelAction()
	{
		$fnum 	= $this->get('nums');
		$nums   = "'".str_replace(',',"','",$fnum)."'";
		$where  = "`filenum` in ($nums)";
		$rows 	= m('file')->getall($where);
		if(!$rows)return returnerror('文件记录不存在');
		foreach($rows as $k=>$frs)$this->filedel($frs);
		return returnsuccess();
	}
	private function filedel($frs)
	{
		$filepath  = $frs['filepath'];
		$thumbpath = $frs['thumbpath'];
		$pdfpath   = $frs['pdfpath'];
		$filepathout    = $frs['filepathout'];
		$thumbpathout   = $frs['thumbpathout'];
		if(!isempt($filepath) && file_exists($filepath))unlink($filepath);
		if(!isempt($thumbpath) && file_exists($thumbpath))unlink($thumbpath);
		if(!isempt($pdfpath) && file_exists($pdfpath))unlink($pdfpath);
		
		if(getconfig('qcloudCos_autoup')){
			if(!isempt($filepathout)){
				$fluj = explode('myqcloud.com', $filepathout);
				$path = arrvalue($fluj,1);
				if($path)c('qcloudCos')->delFile(substr($path,1));
			}
			if(!isempt($thumbpathout)){
				$fluj = explode('myqcloud.com', $thumbpathout);
				$path = arrvalue($fluj,1);
				if($path)c('qcloudCos')->delFile(substr($path,1));
			}
		}
		m('file')->delete($frs['id']);
		$rows 			= m('file')->getall('`oid`='.$frs['id'].'');
		foreach($rows as $k=>$rs)$this->filedel($rs);
	}
}